package com.tensquare.qa.controller;

import com.netflix.ribbon.proxy.annotation.Hystrix;
import com.tensquare.qa.client.LabelClient;
import com.tensquare.qa.pojo.Problem;
import com.tensquare.qa.service.ProblemService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.awt.*;
import java.util.List;

/**
 * Created by WF on 2020/7/22 15:45
 */
@RestController
@RequestMapping("problem")
public class ProblemController {
    @Autowired
    private ProblemService problemService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private LabelClient labelClient;

    private static final String URL = "http://TENSQUARE-BASE";
    //1.最新问题列表
    @GetMapping("newlist/{labelId}/{page}/{size}")
    public Result newlist(@PathVariable String labelId,@PathVariable int page,@PathVariable int size){
        //1.1)查询最新问题列表
        PageResult<Problem> problems = problemService.newlist(labelId,page,size);
        //1.2)返回
        return new Result(true, StatusCode.OK,"查询最新问题列表成功！",problems);
    }
    //2.查询热门问题列表
    @GetMapping("hotlist/{labelId}/{page}/{size}")
    public Result hotlist(@PathVariable String labelId,@PathVariable int page,@PathVariable int size){
        //2.1)查询热门问题列表
        PageResult<Problem> hotlist = problemService.hotList(labelId,page,size);
        //2.2)返回
        return new Result(true,StatusCode.OK,"查询热门问题列表成功！",hotlist);
    }
    //3.查询所有问题列表
    @GetMapping("list")
    public Result findAll(){
        List<Problem> problemList = problemService.findAll();
        return new Result(true,StatusCode.OK,"查询所有问题列表成功！",problemList);
    }
    //4.使用restTemplate调用标签列表
    @GetMapping("label/list")
    public Result list(){
        Result result = restTemplate.getForObject(URL + "/label", Result.class);
        return result;
    }
    //5.使用OpenFeign调用base微服务
    @GetMapping("label/{id}")
    public Result findById(@PathVariable String id){
        return labelClient.getById(id);
    }


}
